--
-- Author: mm
-- Date: 2018-04-17 16:08:30
--
-- FirstTargetRender
-- 指南一级菜单render

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.AuctionFirstTargetRender))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui["Panel"]:getContentSize())
    self._ui["Panel/selected_btn"]:setTouchEnabled( false )
    self._ui["Panel/selected_btn"]:setSwallowTouches( false )
    self._ui["Panel"]:addTouchEventListener(function( sender, eventType )
            if eventType == ccui.TouchEventType.ended then
                self:setSelected( not self:isSelected() )
                executeFunc(self._onClick, self)
            end
        end)
    self._ui.img_1:setVisible( false )
    self._ui.img_2:setVisible( false )
    self._isUnfold = false --是否是展开
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        local data = self._data
        self._ui.name:setString( L(data.name) )
        self:_refreshOpenStatusVisible( false )
    end
end

-- 是否选中
function ClassRef:isSelected()
    return not self._ui["Panel/selected_btn"]:isBright()
end

function ClassRef:setSelected( b )
    if b then
        self._ui.name:setTextColor( cc.c3b( 255, 225, 132 ) )
        if 1 < #self._data.subDatas then -- 1 < 子菜单数量？
            self._ui["Panel"]:setOpacity( 255 )
        else
            self._ui["Panel"]:setOpacity( 0 )
        end
    else
        self._ui.name:setTextColor( cc.c3b( 109, 109, 109 ) )
        self._ui["Panel"]:setOpacity( 0 )
    end
    self:_refreshOpenStatusVisible( b )
    self._ui["Panel/selected_btn"]:setBright( not b )
end

--是否设置为展开
function ClassRef:setUnfold(isUnfold)
    self._isUnfold = isUnfold
    if isUnfold then
        --如果是展开
        self._ui["Panel/img_1"]:setVisible(true)
        self._ui["Panel/img_2"]:setVisible(false)
    else
        --如果是展开
        self._ui["Panel/img_1"]:setVisible(false)
        self._ui["Panel/img_2"]:setVisible(true)
    end
end

--列表当前是否是展开
function ClassRef:isUnfold()
    return self._isUnfold
end

-- 私有方法
-- 根据子菜单个数
-- 决定展开标志是否显示
function ClassRef:_refreshOpenStatusVisible( selected )
    self._ui.img_1:setVisible( false )
    self._ui.img_2:setVisible( false )
    if self._data then
        if 1 < #self._data.subDatas then -- 1 < 子菜单数量？
            self._ui.img_1:setVisible( selected )
            self._ui.img_2:setVisible( not selected )
        end
    end
end

function ClassRef:setClickHandler( func )
    self._onClick = func
end

return ClassRef
